<template>
  <div class="wepage_list">
    <el-row>
      <el-col :span="8"><el-input v-model="wd" placeholder="输入标题关键字搜索" @keyup.enter.native="submitSearch"></el-input></el-col>
      <el-col :span="3" style="margin-left:10px"><el-button type="primary" @click="submitSearch">搜索</el-button></el-col>
    </el-row>
    <el-form :inline="true">
      <el-form-item label="内容性质">
        <el-select v-model="attrId" clearable>
          <el-option v-for="item,n in attrs" :value="item.id" :label="item.name" :key="item.id.toString()"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="tb-head">
      <el-button @click="$router.push({name:'WePage', params: {id: 'new'}})" icon="plus">新建微页面</el-button>
    </div>
    <el-table v-loading="tbLoading" element-loading-text="拼命加载中" :data="pageList.list" stripe class="table">
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="二维码" class-name="is-center">
        <template slot-scope="scope">
          <el-popover
            placement="bottom"
            width="100"
            popper-class="popper_qr"
            trigger="click">
            <img class="img_qr" :src="`data:image/png;base64,${scope.row.qrCode}`" alt="">
            <i class="icon icon-erweima" slot="reference"></i>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="内容性质" prop="attrName" class-name="is-center"></el-table-column>
      <el-table-column label="查看权限/审核状态" class-name="is-center">
        <template slot-scope="scope">
          <span v-if="scope.row.commonType">公共</span>
          <span v-if="scope.row.commonType&&scope.row.groupType">,</span>
          <span v-if="scope.row.groupType">事业部</span>
          <span v-if="!scope.row.commonType&&!scope.row.groupType">自己可见</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" class-name="is-center" prop="createUser">
      </el-table-column>
      <el-table-column label="创建时间" class-name="is-center" width="180">
        <template slot-scope="scope">
          {{dateFormat(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200"  class-name="is-center">
        <template slot-scope="scope">
          <el-button type="text" class="copy" :data-clipboard-text="`${baseUrl}/content/micro/preview/${scope.row.id}`">复制链接<i class="icon icon-lianjie"></i></el-button>
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-caret-bottom el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="scope.row.publishStatus" @click.native="$router.push({name: 'minPageFirst', params: {id: scope.row.id}})">报表</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push({name:'WePage', params: {id: scope.row.id}, query: {cs: 1}})">使用</el-dropdown-item>
              <el-dropdown-item><a :href="`${baseUrl}/content/micro/preview/${scope.row.id}`" target="_blank" style="display:block;text-decoration:none;color:#5e6b7c">预览</a></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page.sync="page"
        :page-size.sync="size"
        layout="sizes, total, prev, pager, next, jumper"
        :page-sizes="[10, 30, 50, 100]"
        :total="pageList.page.total"
        @current-change="submitSearch()"
        @size-change="handleSizeChange">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  import {mapActions, mapState} from 'vuex'
  import config from '@/config/env'
  import Clipboard from 'clipboard'
  export default {
    data () {
      return {
        wd: '',
        attrId: '',
        page: 1,
        size: 10,
        tbLoading: false
      }
    },
    mounted () {
      this.initPage()
      const clipboard = new Clipboard('.copy')
      clipboard.on('success', () => {
        this.$message.success('复制成功')
      })
    },
    computed: {
      ...mapState({
        attrs: state => state.contentManage.contentType,
        pageList: state => state.contentManage.wePageList
      }),
      baseUrl () {
        return config.baseUrl2
      }
    },
    methods: {
      ...mapActions(['getWePageList2', 'getContentTypes']),
      initPage () {
        this.getContentTypes()
        this.submitSearch()
      },
      // 日期格式化
      dateFormat (date) {
        return this.$date.format(new Date(date), 'YYYY-MM-DD hh:mm:ss')
      },
      // 搜索
      async submitSearch () {
        this.tbLoading = true
        await this.getWePageList2({
          title: this.wd,
          attrId: this.attrId,
          authStatus: 3,
          page: this.page,
          size: this.size
        })
        this.tbLoading = false
      },
      // 改变分页大小
      handleSizeChange (size) {
        this.size = size
        this.submitSearch()
      }
    }
  }
</script>
<style lang="scss">
  .wepage_list{
    .el-row{margin-bottom:15px}
    .el-dropdown-link{color:#20a0ff}
    .el-table{
      .icon-erweima{font-size:20px;cursor:pointer;}
      .icon-lianjie{display:inline-block;margin:1px 3px 0 0;font-size:12px;vertical-align:top}
      .el-dropdown{cursor:pointer;}
    }
  }
  .popper_qr{
    &{min-width:auto;}
    .img_qr{margin:-10px;width:120px;height:120px;}
  }
</style>
